package com.study.chapter06;

import com.study.entity.Event;
import com.study.chapter05.source.ClickSource;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.windowing.ProcessWindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;

import java.sql.Timestamp;
import java.util.HashSet;
import java.util.Set;

/**
 * @Description:
 * @Author: LiuQun
 * @Date: 2022/8/3 22:14
 */
public class ProcessWindowFunctionTest {
    public static void main(String[] args) throws Exception {
        //环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        DataStreamSource<Event> stream = env.addSource(new ClickSource());
        stream.print("data");

        //设置水位线
        SingleOutputStreamOperator<Event> watermarkOpt = stream.assignTimestampsAndWatermarks(
                WatermarkStrategy.<Event>forMonotonousTimestamps()
                        .withTimestampAssigner((data, l) -> data.timestamp)
        );
        //设置窗口
        SingleOutputStreamOperator<String> resultOpt = watermarkOpt
                //这个key的数据类型就是ProcessWindowFunction中的key的类型
                .keyBy(data -> true)
                .window(TumblingEventTimeWindows.of(Time.seconds(10)))
                .process(new UVCountByWindow());


        resultOpt.print();

        env.execute();
    }

    /**
     * 自定义处理窗口函数，这个key的数据类型就是分组keyBy对应的字段的数据类型
     */
    public static class UVCountByWindow extends ProcessWindowFunction<Event, String, Boolean, TimeWindow> {

        @Override
        public void process(Boolean aBoolean, Context context, Iterable<Event> elements, Collector<String> out) throws Exception {
            Set<String> userSet = new HashSet<>();
            //elements是缓存的Event的数据集合
            elements.forEach(data -> userSet.add(data.user));

            //获取窗口中的一些信息
            long start = context.window().getStart();
            long end = context.window().getEnd();
            //输出内容
            out.collect("窗口: " + new Timestamp(start) + " ~ " + new Timestamp(end)
                    + " 的独立访客数量是：" + userSet.size());

        }
    }
}
